var CATEGORY_REQUEST = 0; 
var IMAGE_REQUEST = 0; 

/*
 * 
 *
 */
function swap_editable(input,editable)
{
	input.className = 'txt ' +  ((editable) ? 'edit_mode' : 'read_mode');
}

function paginator(json)
{
	var order_by = 'title_es';
	var pag = json.paginator;
	var ul = $('paginator');
	var lis = $A(ul.childNodes);
	lis.each(function(item){
			ul.removeChild(item);
			item = null;
		}
	);

	var current = pag.current;
	var range   = pag.range;

  var tx = document.createElement('p');
	tx.innerHTML = 'Paginas:';
	var li = document.createElement('li');

	li.appendChild(tx);
	ul.appendChild(li);

	range.each(function(item)
	{
    var tx = document.createElement('p');
		tx.innerHTML = item;

		var li = document.createElement('li');
		li.className = 'page';

		if(current == item)
		{
			li.className = 'selected';
		}

		li.onclick = function(){
			var page = $('item_page_var');
			page.value = item;
			items_load(item);
		};		

		li.appendChild(tx);
		ul.appendChild(li);
	});
}

function display_success_message(json)
{
	$("msgs").innerHTML = json.msg;
  new Effect.Appear("msgs", {duration:1, from:0.1, to:1.0});
	setTimeout('hide_success_message()',3000);
}

function hide_success_message()
{
  new Effect.Fade("msgs", {duration:1});
}


/***************************************************************************************************
 *
 *  PRODUCT EDIT
 *
 ***************************************************************************************************/

/**
 *
 *
 */
function product_init()
{
  product_form_init();
	LANGS.each(function (item){
    var botton = $('lang_'+item);
		$('li_'+item).className = 'noactive';

		botton.onclick = function(){
		  botton.className = 'active';
			LANGS.each(function(lang){
			  $('edit_'+lang).style.display = (item == lang) ? '' : 'none';
				$('li_'+lang).className = (item == lang) ? 'active' : 'noactive';
			})
		}
	});

	var lang = LANGS[0];
	$('edit_'+lang).style.display = '';
	$('li_'  +lang).className = 'active';
	categories_load(ITEM_ID);
	images_init(ITEM_ID);
}

/*
 *
 *
 */
function product_form_init()
{
	var save   = $('product_save');
	var cancel = $('product_cancel');
	var del    = $('product_delete');

	save.onclick = function()
	{
    var form = $('product_form')
    var price = document.createElement('input');
		price.type = 'text'; 
		price.name = 'price'; 
		price.value = $F('product_price')
		
	  var pstat = document.createElement('input');
		pstat.type = 'text'; 
		pstat.name = 'visibility'; 
		pstat.value = $F('product_visibility')

		form.appendChild(pstat);
		form.appendChild(price);
		form.submit();
	}

	cancel.onclick = function()
	{
		if(confirm('Salir?'))
			document.location = 'index.php?c=ajax_products';
	}

	del.onclick = function()
	{
		if(confirm('Eliminar producto?'))
			document.location = 'index.php?c=ajax_products&a=product_delete&id=' + ITEM_ID;
	}

}

/***************************************************************************************************
 *
 *  PRODUCT LIST 
 *
 ***************************************************************************************************/


/**
 *
 *
 */
function item_list_refresh(json)
{
	paginator(json);
	items = json.items;
	// Sortable.destroy('item_list');

	var ul = $('items_list');
	var lis = $A(ul.childNodes);
	lis.each(function(item){
			ul.removeChild(item);
			item = null;
		}
	);

	items.each(function(item){

		var ip = document.createElement('input');
		ip.type  = 'hidden';
		ip.value = item.order;
		ip.name  = 'items[' + item.id + ']';
		ip.id    = 'input_' + item.id;

    var im = document.createElement('img');
    im.src = '../public/icons/16x16/actions/edit.png';
    im.onclick = function(){
      document.location = 'index.php?c=ajax_' + ITEM_TYPE + '&a=item&id=' + item.id ;
    }

    var tx = document.createElement('text');
		tx.innerHTML = item.title;

		var li = document.createElement('li');
		li.appendChild(ip);
		li.appendChild(im);
		li.appendChild(tx);
		li.name = item.id;
		ul.appendChild(li);
	})

	Sortable.create('items_list');
}




/*
 *
 *
 */
function items_form_init()
{
	var save = $('item_save_order');
	save.onclick = function (){
		var index = 0;
		var ul = $('items_list');
		var lis = $A(ul.childNodes);

		lis.each(function(item){
				$('input_' + item.name).value = index;
				index = index + 1;	
		});
    
	  var form = $('item_order_form');
	  form.method = 'post';
	  form.action = 'index.php?c=ajax_items&a=items_save_order';
	  form.target = 'nodisplay';
	  form.submit();
  }

	var vsty = $('item_visibility');
	vsty.onchange = function(){
		$('item_visibility_var').value = $F('item_visibility');
		var page = $('item_page_var');
		page.value = 0;
		items_load();
	}

	var order_title = $('item_title_order');
	order_title.onclick = function (){
		$('item_order_var').value= 'title_es';
		items_load();
		return false;
	}

	var order_order = $('item_order_order');
	order_order.onclick = function (){
		$('item_order_var').value = 'order';
		items_load();
		return false;
	}
}

/*
 *
 *
 */
function items_load()
{
	new Ajax.Request('index.php?c=ajax_items&a=items&type=' + ITEM_TYPE + '&sort=' + $F('item_order_var') + '&vsty=' + $F('item_visibility_var') + '&pge=' + $F('item_page_var'), {
  	method:'get',
  	onSuccess: function(transport){
     	var json = transport.responseText.evalJSON();
			item_list_refresh(json);
   	}
	});
	items_form_init();
}

/*
 *
 *
 */
function items_init()
{
	items_load();
}

/***************************************************************************************************
 *
 *  POST EDIT
 *
 ***************************************************************************************************/

/*
 *
 *
 */
function posts_init()
{
  post_form_init();
	LANGS.each(function (item){
    var botton = $('lang_'+item);
		$('li_'+item).className = 'noactive';

		botton.onclick = function(){
		  botton.className = 'active';
			LANGS.each(function(lang){
			  $('edit_'+lang).style.display = (item == lang) ? '' : 'none';
				$('li_'+lang).className = (item == lang) ? 'active' : 'noactive';
			})
		}
	});

	var lang = LANGS[0];
	$('edit_'+lang).style.display = '';
	$('li_'  +lang).className = 'active';
	categories_load(ITEM_ID);
	images_init(ITEM_ID);
}

/*
 *
 *
 */
function post_form_init()
{
	var save   = $('post_save');
	var cancel = $('post_cancel');
	var del    = $('post_delete');

	save.onclick = function()
	{
    var form = $('post_form')
		
	  var pstat = document.createElement('input');
		pstat.type = 'text'; 
		pstat.name = 'visibility'; 
		pstat.value = $F('post_visibility')

	  var pcmt = document.createElement('input');
		pcmt.type = 'text'; 
		pcmt.name = 'comments'; 
		pcmt.value = $F('post_comments')

		form.appendChild(pstat);
		form.appendChild(pcmt);
		form.submit();
	}

	cancel.onclick = function()
	{
		if(confirm('Salir?'))
			document.location = 'index.php?c=ajax_posts';
	}

	del.onclick = function()
	{
		if(confirm('Eliminar posto?'))
			document.location = 'index.php?c=ajax_posts&a=post_delete&id=' + ITEM_ID;
	}
}

/***************************************************************************************************
 *
 *  PAGE EDIT
 *
 ***************************************************************************************************/

/*
 *
 *
 */
function pages_init()
{
  page_form_init();
	LANGS.each(function (item){
    var botton = $('lang_'+item);
		$('li_'+item).className = 'noactive';

		botton.onclick = function(){
		  botton.className = 'active';
			LANGS.each(function(lang){
			  $('edit_'+lang).style.display = (item == lang) ? '' : 'none';
				$('li_'+lang).className = (item == lang) ? 'active' : 'noactive';
			})
		}
	});

	var lang = LANGS[0];
	$('edit_'+lang).style.display = '';
	$('li_'  +lang).className = 'active';
	categories_load(ITEM_ID);
	images_init(ITEM_ID);
}

/*
 *
 *
 */
function page_form_init()
{
	var save   = $('page_save');
	var cancel = $('page_cancel');
	var del    = $('page_delete');

	save.onclick = function()
	{
    var form = $('page_form')
		
	  var pstat = document.createElement('input');
		pstat.type = 'text'; 
		pstat.name = 'visibility'; 
		pstat.value = $F('page_visibility')

		var rsid = document.createElement('input');
		rsid.type = 'text'; 
		rsid.name = 'resource_id'; 
		rsid.value = $F('page_rosurce_id')

		form.appendChild(pstat);
		form.appendChild(rsid);
		form.submit();
	}

	cancel.onclick = function()
	{
		if(confirm('Salir?'))
			document.location = 'index.php?c=ajax_pages';
	}

	del.onclick = function()
	{
		if(confirm('Eliminar page?'))
			document.location = 'index.php?c=ajax_pages&a=page_delete&id=' + ITEM_ID;
	}
}


